﻿using System;
using com.unioncreations.ibuddy;
using System.Threading;
using TFSBuddy.IBuddy;
using TFSBuddy.Settings;
using System.Windows.Media;
using System.Windows.Shell;
using System.Windows;

namespace TFSBuddy
{
    public class IBuddyHelper
    {
        private static iBuddy[] buddyList;
        public static iBuddy[] GetBuddyList()
        {
            buddyList = iBuddyFactory.Instance.GetActiveIBuddies();
            for (int i = 0; i < buddyList.Length; i++)
            {
                buddyList[i].Open();
            }
            
            return buddyList;
        }

        private static void setIconOverLay(Window window,string Color)
        {
            if (window != null)
            {
                DrawingBrush visual = window.Resources[Color] as DrawingBrush;
                TaskbarItemInfo taskbarItem = new TaskbarItemInfo();
                if (visual != null)
                {
                    DrawingImage image = new DrawingImage(visual.Drawing);
                    taskbarItem.Overlay = image;
                }
                else
                {
                    taskbarItem.Overlay = null;
                }
                window.TaskbarItemInfo = taskbarItem;
            }
        }

        public static void ActivateIBuddyEvent(iBuddyStateList states,int repeat,iBuddy[] buddyList,Window window)
        {
            try
            {
                if (buddyList != null)
                {
                    for (int i = 0; i < buddyList.Length; i++)
                    {
                        for (int r = 0; r < repeat; r++)
                        {
                            for (int a = 0; a < states.Count; a++)
                            {
                                var s = states[a];
                                buddyList[i].PlayEffect(GetBodyCommand(s), GetHeadCommand(s), GetWingCommand(s), GetHeartEffect(s));
                                setIconOverLay(window, s.HeadEffect.ToString());


                                switch (s.DurationEffect)
                                {
                                    case iBuddyDurationEffects.Duration100:
                                        Thread.Sleep(100);
                                        break;
                                    case iBuddyDurationEffects.Duration1000:
                                        Thread.Sleep(1000);
                                        break;
                                    case iBuddyDurationEffects.Duration500:
                                        Thread.Sleep(500);
                                        break;
                                }
                                if (a == states.Count - 1 && s.DurationEffect != iBuddyDurationEffects.Forever)
                                {
                                    buddyList[0].Reset();
                                    setIconOverLay(window, "Off");
                                }
                            }
                        }
                        //To get the Forever option to work we cannot close the connetction.
                        //buddyList[i].Close();
                    }
                }
            }
            catch { }
        }
        private static iBuddyHeadEffect GetHeadCommand(iBuddyState s)
        {
            switch (s.HeadEffect)
            {
                case iBuddyHeadEffects.Off:
                    return iBuddyHeadEffect.OFF;
                case iBuddyHeadEffects.Blue:
                    return iBuddyHeadEffect.LIGHT_BLUE;
                case iBuddyHeadEffects.Cyan:
                    return iBuddyHeadEffect.LIGHT_CYAN;
                case iBuddyHeadEffects.Green:
                    return iBuddyHeadEffect.LIGHT_GREEN;
                case iBuddyHeadEffects.Magenta:
                    return iBuddyHeadEffect.LIGHT_MAGENTA;
                case iBuddyHeadEffects.Red:
                    return iBuddyHeadEffect.LIGHT_RED;
                case iBuddyHeadEffects.White:
                    return iBuddyHeadEffect.LIGHT_WHITE;
                case iBuddyHeadEffects.Yellow:
                    return iBuddyHeadEffect.LIGHT_YELLOW;
            }
            return null;
        }
        private static iBuddyBodyEffect GetBodyCommand(iBuddyState s)
        {
            //ExecuteBodyCommand
            switch (s.BodyEffect)
            {
                case iBuddyBodyEffects.Off:
                    return iBuddyBodyEffect.OFF;
                case iBuddyBodyEffects.TurnRight:
                    return iBuddyBodyEffect.TURN_RIGHT;
                case iBuddyBodyEffects.TurtLeft:
                    return iBuddyBodyEffect.TURN_LEFT;
            }
            return null;
        }
        private static iBuddyWingEffect GetWingCommand(iBuddyState s)
        {
            //ExecuteWingCommand
            switch (s.WingEffect)
            {
                case iBuddyWingEffects.Off:
                    return iBuddyWingEffect.OFF;
                case iBuddyWingEffects.Down:
                    return iBuddyWingEffect.WING_DOWN;
                case iBuddyWingEffects.Up:
                    return iBuddyWingEffect.WING_UP;
            }
            return null;
        }
        private static iBuddyHeartEffect GetHeartEffect(iBuddyState s)
        {
            //ExecuteHeartEffect
            switch (s.HeartEffect)
            {
                case iBuddyHeartEffects.Off:
                    return iBuddyHeartEffect.OFF;
                case iBuddyHeartEffects.On:
                    return iBuddyHeartEffect.LIGHT;
            }
            return null;
        }
    }
}
